package com.step.scene.light;

import javax.microedition.khronos.opengles.GL10;

import com.step.math.Vector;
import com.step.mesh.Color;

public class DirectionLight {
	float[] ambient = { 0.2f, 0.2f, 0.2f, 1.0f };
	float[] diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
	float[] specular = { 0.0f, 0.0f, 0.0f, 1.0f };
	float[] position = { 0, 0, -1, 0};
	int lastLightId = 0;

	public void setAmbient(float r, float g, float b, float a) {
		ambient[0] = r;
		ambient[1] = g;
		ambient[2] = b;
		ambient[3] = a;
	}
	
	public void setAmbient(Color color){
		setAmbient((float)color.r / 255f, 
				(float)color.g / 255f, 
				(float)color.b / 255f, 
				(float)color.a / 255f);
	}

	public void setDiffuse(float r, float g, float b, float a) {
		diffuse[0] = r;
		diffuse[1] = g;
		diffuse[2] = b;
		diffuse[3] = a;
	}

	public void setDiffuse(Color color){
		setAmbient((float)color.r / 255f, 
				(float)color.g / 255f, 
				(float)color.b / 255f, 
				(float)color.a / 255f);
	}
	
	public void setSpecular(float r, float g, float b, float a) {
		specular[0] = r;
		specular[1] = g;
		specular[2] = b;
		specular[3] = a;
	}

	public void setSpecular(Color color){
		setAmbient((float)color.r / 255f, 
				(float)color.g / 255f, 
				(float)color.b / 255f, 
				(float)color.a / 255f);
	}
	
	public void setDirection(float x, float y, float z) {
		position[0] = -x;
		position[1] = -y;
		position[2] = -z;
	}
	
	public void setPosition(Vector position){
		this.position[0] = -position.getX();
		this.position[1] = -position.getY();
		this.position[2] = -position.getZ();
	}

	public void enable(GL10 gl, int lightId) {
		gl.glEnable(lightId);
		gl.glLightfv(lightId, GL10.GL_AMBIENT, ambient, 0);
		gl.glLightfv(lightId, GL10.GL_DIFFUSE, diffuse, 0);
		gl.glLightfv(lightId, GL10.GL_SPECULAR, specular, 0);
		gl.glLightfv(lightId, GL10.GL_POSITION, position, 0);
		lastLightId = lightId;
	}

	public void disable(GL10 gl) {
		gl.glDisable(lastLightId);
	}
}
